use axum::{Json, http::HeaderMap};
use com::{wx::{claims::get_id, user_session::create_token}, sea_orm::connect, code};
use serde_json::{Value, json};
use sea_orm::{ entity::*, query::* };
use model::wx_user::{ Entity, Column };
// 
pub async fn get_token_key(header:HeaderMap) -> Result<Option<Value>, Json<Value>>{
    let id = get_id(header)?;
    let conn = connect().await?;
    let result = Entity::find_by_id(id)
    .columns([Column::Id, Column::Ident, Column::Casc, Column::Veri])
    .into_json()
    .one(&conn).await;
    match result {
        Ok(f)=> Ok(f),
        Err(e) => Err(code::sys_(e.to_string()))
    }
}
// token 过期，以旧换新
pub async fn login(header:HeaderMap) -> Result<Json<Value>, Json<Value>>{
    // let c = get_user(headers)?;
    if let Some(f) = get_token_key(header).await? {
        let rs = create_token(f)?;
        code::data(json!(rs))
    }else{
        code::fail(204) // 用户不存在
    }
}